home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / term43-source.lha / Extras / Source / term-Source.lha / termSerial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-18  |  38.3 KB  |  2,094 lines

  1. /*
  2. **    termSerial.c
  3. **
  4. **    Serial driver support routines
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* Local copy of serial driver name and unit number. */
  13.  
  14. STATIC UBYTE __far    SerialDevice[40];
  15. STATIC LONG        UnitNumber = -1;
  16.  
  17. STATIC BOOLEAN        SerialLocked = FALSE;
  18.  
  19.     /* SetFlags():
  20.      *
  21.      *    Set the contents of a serial device request according
  22.      *    to the current configuration settings.
  23.      */
  24.  
  25. STATIC BYTE __regargs
  26. SetFlags(VOID)
  27. {
  28.     return(SetBothSerialAttributes(
  29.         SERA_Baud,        Config -> SerialConfig -> BaudRate,
  30.         SERA_BreakTime,        Config -> SerialConfig -> BreakLength,
  31.         SERA_BitsPerChar,    Config -> SerialConfig -> BitsPerChar,
  32.         SERA_StopBits,        Config -> SerialConfig -> StopBits,
  33.         SERA_BufferSize,    Config -> SerialConfig -> SerialBufferSize,
  34.         SERA_Parity,        Config -> SerialConfig -> Parity,
  35.         SERA_Handshaking,    Config -> SerialConfig -> HandshakingProtocol,
  36.         SERA_HighSpeed,        Config -> SerialConfig -> HighSpeed,
  37.         SERA_Shared,        Config -> SerialConfig -> Shared,
  38.     TAG_DONE));
  39. }
  40.  
  41.     /* SendBreak():
  42.      *
  43.      *    Transmit a break signal.
  44.      */
  45.  
  46. VOID
  47. SendBreak()
  48. {
  49.     BYTE OldStatus = Status;
  50.  
  51.     Status = STATUS_BREAKING;
  52.  
  53.     DoSerialCmd(SDCMD_BREAK);
  54.  
  55.     Status = OldStatus;
  56. }
  57.  
  58.     /* HangUp():
  59.      *
  60.      *    Hang up the line.
  61.      */
  62.  
  63. VOID
  64. HangUp()
  65. {
  66.     BYTE OldStatus = Status;
  67.  
  68.     StopSerialWrite();
  69.  
  70.     Status = STATUS_HANGUP;
  71.  
  72.         /* Are we to drop the DTR line
  73.          * before sending the hangup
  74.          * string?
  75.          */
  76.  
  77.     if(Config -> ModemConfig -> DropDTR && WriteRequest)
  78.     {
  79.         /* Let's be nice and try to transmit the
  80.          * `drop the line' command before
  81.          * trying to close and reopen the driver.
  82.          */
  83.  
  84.         WriteRequest -> IOSer . io_Command    = SIOCMD_SETCTRLLINES;
  85.         WriteRequest -> IOSer . io_Offset    = SIOB_DTRF;
  86.         WriteRequest -> IOSer . io_Length    = 0;
  87.  
  88.             /* Transmit the command. */
  89.  
  90.         if(!DoIO(WriteRequest))
  91.         {
  92.                 /* Wait a bit... */
  93.  
  94.             WaitTime(1,0);
  95.  
  96.                 /* Raise the line again. */
  97.  
  98.             WriteRequest -> IOSer . io_Command    = SIOCMD_SETCTRLLINES;
  99.             WriteRequest -> IOSer . io_Offset    = SIOB_DTRF;
  100.             WriteRequest -> IOSer . io_Length    = SIOB_DTRF;
  101.  
  102.             DoIO(WriteRequest);
  103.         }
  104.         else
  105.         {
  106.                 /* Do it the standard way: close and reopen
  107.                  * the serial driver (the serial.device is
  108.                  * supposed to drop the DTR line when closed).
  109.                  */
  110.  
  111.             if(!DropDTR())
  112.             {
  113.                 if(!MyEasyRequest(Window,LocaleString(MSG_TERMMAIN_FAILED_TO_REOPEN_UNIT_TXT),LocaleString(MSG_TERMMAIN_IGNORE_QUIT_TXT),Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber))
  114.                     MainTerminated = TRUE;
  115.             }
  116.         }
  117.     }
  118.  
  119.         /* Transmit the hangup command. */
  120.  
  121.     if(Config -> ModemConfig -> ModemHangup[0])
  122.         SerialCommand(Config -> ModemConfig -> ModemHangup);
  123.  
  124.         /* Reset to old status. */
  125.  
  126.     Status = OldStatus;
  127. }
  128.  
  129.     /* DropDTR():
  130.      *
  131.      *    Drop the data terminal ready signal (i.e. close, wait a bit
  132.      *    and then reopen the serial drive).
  133.      */
  134.  
  135. BYTE
  136. DropDTR()
  137. {
  138.         /* Finish all serial read activity. */
  139.  
  140.     ClearSerial();
  141.  
  142.         /* Do we have any channels to work with? */
  143.  
  144.     if(ReadRequest && WriteRequest)
  145.     {
  146.             /* Close the device. */
  147.  
  148.         CloseDevice(ReadRequest);
  149.  
  150.             /* Wait a bit. */
  151.  
  152.         WaitTime(1,0);
  153.  
  154.             /* Reopen the driver. */
  155.  
  156.         if(!OpenDevice(Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,ReadRequest,0))
  157.         {
  158.             struct MsgPort *WritePort = WriteRequest -> IOSer . io_Message . mn_ReplyPort;
  159.  
  160.             ResetSerialRead();
  161.             ResetSerialWrite();
  162.  
  163.                 /* Fill in the rest. */
  164.  
  165.             CopyMem(ReadRequest,WriteRequest,sizeof(struct IOExtSer));
  166.  
  167.             WriteRequest -> IOSer . io_Message . mn_ReplyPort = WritePort;
  168.  
  169.             SetFlags();
  170.  
  171.             DoSerialCmd(SDCMD_SETPARAMS);
  172.  
  173.                 /* Restart read activity. */
  174.  
  175.             RestartSerial(FALSE);
  176.  
  177.             return(TRUE);
  178.         }
  179.         else
  180.             DeleteSerial();
  181.     }
  182.     else
  183.     {
  184.         DeleteSerial();
  185.  
  186.         return(TRUE);
  187.     }
  188.  
  189.     return(FALSE);
  190. }
  191.  
  192.     /* CopyWriteFlags():
  193.      *
  194.      *    Update configuration with serial settings.
  195.      */
  196.  
  197. VOID
  198. CopyWriteFlags()
  199. {
  200.     ULONG    Baud,
  201.         BreakTime,
  202.         BitsPerChar,
  203.         StopBits,
  204.         BufferSize,
  205.         Parity,
  206.         Handshaking,
  207.         HighSpeed,
  208.         Shared;
  209.  
  210.     GetSerialWriteAttributes(
  211.         SERA_Baud,        &Baud,
  212.         SERA_BreakTime,        &BreakTime,
  213.         SERA_BitsPerChar,    &BitsPerChar,
  214.         SERA_StopBits,        &StopBits,
  215.         SERA_BufferSize,    &BufferSize,
  216.         SERA_Parity,        &Parity,
  217.         SERA_Handshaking,    &Handshaking,
  218.         SERA_HighSpeed,        &HighSpeed,
  219.         SERA_Shared,        &Shared,
  220.     TAG_DONE);
  221.  
  222.     Config -> SerialConfig -> BaudRate        = Baud;
  223.     Config -> SerialConfig -> BreakLength        = BreakTime;
  224.     Config -> SerialConfig -> BitsPerChar        = BitsPerChar;
  225.     Config -> SerialConfig -> StopBits        = StopBits;
  226.     Config -> SerialConfig -> SerialBufferSize    = BufferSize;
  227.     Config -> SerialConfig -> Parity        = Parity;
  228.     Config -> SerialConfig -> HandshakingProtocol    = Handshaking;
  229.     Config -> SerialConfig -> HighSpeed        = HighSpeed;
  230.     Config -> SerialConfig -> Shared        = Shared;
  231. }
  232.  
  233.     /* CallMenu(STRPTR Name,ULONG Code):
  234.      *
  235.      *    Call a menu function either through the name of the corresponding
  236.      *    menu item or a menu number.
  237.      */
  238.  
  239. STATIC VOID __inline
  240. CallMenu(STRPTR Name,ULONG Code)
  241. {
  242.     WORD MenuNum = -1,Item = 0,Sub = 0,i;
  243.  
  244.         /* Are we to look for a name? */
  245.  
  246.     if(Name)
  247.     {
  248.         WORD Len = strlen(Name);
  249.  
  250.             /* Scan the menu list... */
  251.  
  252.         for(i = 0 ; TermMenu[i] . nm_Type != NM_END ; i++)
  253.         {
  254.             switch(TermMenu[i] . nm_Type)
  255.             {
  256.                 case NM_TITLE:
  257.  
  258.                     MenuNum++;
  259.                     Item = Sub = 0;
  260.                     break;
  261.  
  262.                 case NM_ITEM:
  263.  
  264.                     Sub = 0;
  265.                     break;
  266.             }
  267.  
  268.                 /* Did we get a valid name string? */
  269.  
  270.             if(TermMenu[i] . nm_Label != NM_BARLABEL)
  271.             {
  272.                     /* Does the name match our template? */
  273.  
  274.                 if(!Strnicmp(TermMenu[i] . nm_Label,Name,Len))
  275.                 {
  276.                     struct MenuItem *MenuItem = ItemAddress(Menu,FULLMENUNUM(MenuNum,Item,Sub));
  277.  
  278.                     if(MenuItem && (MenuItem -> Flags & ITEMENABLED))
  279.                         HandleMenuCode((ULONG)TermMenu[i] . nm_UserData,NULL);
  280.  
  281.                     break;
  282.                 }
  283.             }
  284.  
  285.             switch(TermMenu[i] . nm_Type)
  286.             {
  287.                 case NM_ITEM:
  288.  
  289.                     Item++;
  290.                     break;
  291.  
  292.                 case NM_SUB:
  293.  
  294.                     Sub++;
  295.                     break;
  296.             }
  297.         }
  298.     }
  299.     else
  300.     {
  301.         WORD    TheMenu    =  Code % 100,
  302.             TheItem    = (Code / 100) % 100,
  303.             TheSub    =  Code / 10000;
  304.  
  305.             /* Scan the menu list... */
  306.  
  307.         for(i = 0 ; TermMenu[i] . nm_Type != NM_END ; i++)
  308.         {
  309.             switch(TermMenu[i] . nm_Type)
  310.             {
  311.                 case NM_TITLE:
  312.  
  313.                     MenuNum++;
  314.                     Item = Sub = 0;
  315.                     break;
  316.  
  317.                 case NM_ITEM:
  318.  
  319.                     Sub = 0;
  320.                     break;
  321.             }
  322.  
  323.                 /* Is it the menu number we want? */
  324.  
  325.             if(TheMenu == MenuNum && TheItem == Item && TheSub == Sub)
  326.             {
  327.                 if(TermMenu[i] . nm_Label != NM_BARLABEL)
  328.                 {
  329.                     struct MenuItem *MenuItem = ItemAddress(Menu,FULLMENUNUM(MenuNum,Item,Sub));
  330.  
  331.                     if(MenuItem && (MenuItem -> Flags & ITEMENABLED))
  332.                         HandleMenuCode((ULONG)TermMenu[i] . nm_UserData,NULL);
  333.                 }
  334.  
  335.                 break;
  336.             }
  337.  
  338.             switch(TermMenu[i] . nm_Type)
  339.             {
  340.                 case NM_ITEM:
  341.  
  342.                     Item++;
  343.                     break;
  344.  
  345.                 case NM_SUB:
  346.  
  347.                     Sub++;
  348.                     break;
  349.             }
  350.         }
  351.     }
  352. }
  353.  
  354.     /* SerialCommand(STRPTR String):
  355.      *
  356.      *    Send a command string to the serial line and
  357.      *    interprete the control sequences.
  358.      */
  359.  
  360. VOID __regargs
  361. SerialCommand(STRPTR String)
  362. {
  363.     BYTE    (*  SendLineLocal)(register STRPTR,register LONG);
  364.  
  365.     LONG    Count = 0,i,Len = strlen(String);
  366.  
  367.     BYTE    GotControl    = FALSE,
  368.         GotEscape    = FALSE;
  369.  
  370.     SendLineLocal = SendLine;
  371.  
  372.         /* Scan the string. */
  373.  
  374.     for(i = 0 ; i < Len ; i++)
  375.     {
  376.             /* We are looking for plain characters
  377.              * and the control ('\') and escape
  378.              * ('^') characters.
  379.              */
  380.  
  381.         if(!GotControl && !GotEscape)
  382.         {
  383.                 /* Got a control character,
  384.                  * the next byte will probably be
  385.                  * a command sequence.
  386.                  */
  387.  
  388.             if(String[i] == '\\')
  389.             {
  390.                 GotControl = TRUE;
  391.                 continue;
  392.             }
  393.  
  394.                 /* Got an escape character,
  395.                  * the next byte will be some
  396.                  * kind of control character
  397.                  * (such as XON, XOF, bell, etc.).
  398.                  */
  399.  
  400.             if(String[i] == '^')
  401.             {
  402.                 GotEscape = TRUE;
  403.                 continue;
  404.             }
  405.  
  406.                 /* This tells us to wait another
  407.                  * second before continuing with
  408.                  * the scanning.
  409.                  */
  410.  
  411.             if(String[i] == '~')
  412.             {
  413.                 if(Count)
  414.                 {
  415.                     (*SendLineLocal)(SharedBuffer,Count);
  416.  
  417.                     Count = 0;
  418.                 }
  419.  
  420.                 WaitTime(0,MILLION / 2);
  421.  
  422.                 HandleSerial();
  423.  
  424.                 continue;
  425.             }
  426.  
  427.                 /* Stuff the character into the
  428.                  * buffer.
  429.                  */
  430.  
  431.             SharedBuffer[Count++] = String[i];
  432.         }
  433.         else
  434.         {
  435.                 /* Convert the character to a control
  436.                  * style character (^C, etc.).
  437.                  */
  438.  
  439.             if(GotEscape)
  440.             {
  441.                 if(ToUpper(String[i]) >= 'A' && ToUpper(String[i]) <= '_')
  442.                     SharedBuffer[Count++] = ToUpper(String[i]) - '@';
  443.                 else
  444.                     SharedBuffer[Count++] = String[i];
  445.  
  446.                 GotEscape = FALSE;
  447.             }
  448.  
  449.                 /* The next character represents a command. */
  450.  
  451.             if(GotControl)
  452.             {
  453.                 switch(ToUpper(String[i]))
  454.                 {
  455.                         /* Fall back to default send mode. */
  456.  
  457.                     case '0':
  458.  
  459.                         if(Count)
  460.                         {
  461.                             (*SendLineLocal)(SharedBuffer,Count);
  462.  
  463.                             Count = 0;
  464.                         }
  465.  
  466.                         SendLineLocal = SendLine;
  467.                         break;
  468.  
  469.                         /* Select `direct' send mode. */
  470.  
  471.                     case '1':
  472.  
  473.                         if(Count)
  474.                         {
  475.                             (*SendLineLocal)(SharedBuffer,Count);
  476.  
  477.                             Count = 0;
  478.                         }
  479.  
  480.                         SendLineLocal = SendLineSimple;
  481.                         break;
  482.  
  483.                         /* Select `echo' send mode. */
  484.  
  485.                     case '2':
  486.  
  487.                         if(Count)
  488.                         {
  489.                             (*SendLineLocal)(SharedBuffer,Count);
  490.  
  491.                             Count = 0;
  492.                         }
  493.  
  494.                         if(Config -> ClipConfig -> SendTimeout)
  495.                             SendLineLocal = SendLineEcho;
  496.                         else
  497.                             SendLineLocal = SendLineSimple;
  498.  
  499.                         break;
  500.  
  501.                         /* Select `any echo' send mode. */
  502.  
  503.                     case '3':
  504.  
  505.                         if(Count)
  506.                         {
  507.                             (*SendLineLocal)(SharedBuffer,Count);
  508.  
  509.                             Count = 0;
  510.                         }
  511.  
  512.                         if(Config -> ClipConfig -> SendTimeout)
  513.                             SendLineLocal = SendLineAnyEcho;
  514.                         else
  515.                             SendLineLocal = SendLineSimple;
  516.  
  517.                         break;
  518.  
  519.                         /* Select `prompt' send mode. */
  520.  
  521.                     case '4':
  522.  
  523.                         if(Count)
  524.                         {
  525.                             (*SendLineLocal)(SharedBuffer,Count);
  526.  
  527.                             Count = 0;
  528.                         }
  529.  
  530.                         if(Config -> ClipConfig -> SendTimeout)
  531.                             SendLineLocal = SendLinePrompt;
  532.                         else
  533.                             SendLineLocal = SendLineSimple;
  534.  
  535.                         break;
  536.  
  537.                         /* Select `delay' send mode. */
  538.  
  539.                     case '5':
  540.  
  541.                         if(Count)
  542.                         {
  543.                             (*SendLineLocal)(SharedBuffer,Count);
  544.  
  545.                             Count = 0;
  546.                         }
  547.  
  548.                         if(Config -> ClipConfig -> LineDelay || Config -> ClipConfig -> CharDelay)
  549.                             SendLineLocal = SendLineDelay;
  550.                         else
  551.                             SendLineLocal = SendLineSimple;
  552.  
  553.                         break;
  554.  
  555.                         /* Select `keyboard' send mode. */
  556.  
  557.                     case '6':
  558.  
  559.                         if(Count)
  560.                         {
  561.                             (*SendLineLocal)(SharedBuffer,Count);
  562.  
  563.                             Count = 0;
  564.                         }
  565.  
  566.                         SendLineLocal = SendLineKeyDelay;
  567.                         break;
  568.  
  569.                         /* Translate code. */
  570.  
  571.                     case '*':
  572.  
  573.                         i++;
  574.  
  575.                         while(i < Len && String[i] == ' ')
  576.                             i++;
  577.  
  578.                         if(i < Len)
  579.                         {
  580.                             UBYTE DummyBuffer[5],j = 0,Char;
  581.  
  582.                             if(String[i] >= '0' && String[i] <= '9')
  583.                             {
  584.                                 while(j < 3 && i < Len)
  585.                                 {
  586.                                     Char = String[i++];
  587.  
  588.                                     if(Char >= '0' && Char <= '9')
  589.                                         DummyBuffer[j++] = Char;
  590.                                     else
  591.                                     {
  592.                                         i--;
  593.  
  594.                                         break;
  595.                                     }
  596.                                 }
  597.                             }
  598.                             else
  599.                             {
  600.                                 while(j < 4 && i < Len)
  601.                                 {
  602.                                     Char = ToLower(String[i++]);
  603.  
  604.                                     if((Char >= '0' && Char <= '9') || (Char >= 'a' && Char <= 'z'))
  605.                                         DummyBuffer[j++] = Char;
  606.                                     else
  607.                                     {
  608.                                         i--;
  609.  
  610.                                         break;
  611.                                     }
  612.                                 }
  613.                             }
  614.  
  615.                             DummyBuffer[j] = 0;
  616.  
  617.                             SharedBuffer[Count++] = NameToCode(DummyBuffer);
  618.                         }
  619.  
  620.                         i--;
  621.  
  622.                         break;
  623.  
  624.                         /* Execute an AmigaDOS command. */
  625.  
  626.                     case 'D':
  627.  
  628.                         if(!InRexx)
  629.                         {
  630.                             if(!WeAreBlocking)
  631.                             {
  632.                                 BlockWindows();
  633.  
  634.                                 SendAmigaDOSCommand(&String[i + 1]);
  635.  
  636.                                 ReleaseWindows();
  637.                             }
  638.                             else
  639.                                 SendAmigaDOSCommand(&String[i + 1]);
  640.                         }
  641.  
  642.                         return;
  643.  
  644.                         /* Execute an ARexx command. */
  645.  
  646.                     case 'A':
  647.  
  648.                         if(!InRexx)
  649.                         {
  650.                             if(!WeAreBlocking)
  651.                             {
  652.                                 BlockWindows();
  653.  
  654.                                 SendARexxCommand(&String[i + 1],TRUE);
  655.  
  656.                                 ReleaseWindows();
  657.                             }
  658.                             else
  659.                                 SendARexxCommand(&String[i + 1],TRUE);
  660.                         }
  661.  
  662.                         return;
  663.  
  664.                         /* Add the control character ('\'). */
  665.  
  666.                     case '\\':
  667.  
  668.                         SharedBuffer[Count++] = '\\';
  669.                         break;
  670.  
  671.                         /* This is a backspace. */
  672.  
  673.                     case 'B':
  674.  
  675.                         SharedBuffer[Count++] = '\b';
  676.                         break;
  677.  
  678.                         /* This is a form feed. */
  679.  
  680.                     case 'F':
  681.  
  682.                         SharedBuffer[Count++] = '\f';
  683.                         break;
  684.  
  685.                         /* This is a line feed. */
  686.  
  687.                     case 'N':
  688.  
  689.                         SharedBuffer[Count++] = '\n';
  690.                         break;
  691.  
  692.                         /* Send the current password. */
  693.  
  694.                     case 'P':
  695.  
  696.                         if(Password[0])
  697.                         {
  698.                             if(Count)
  699.                             {
  700.                                 (*SendLineLocal)(SharedBuffer,Count);
  701.  
  702.                                 Count = 0;
  703.                             }
  704.  
  705.                             (*SendLineLocal)(Password,strlen(Password));
  706.                         }
  707.  
  708.                         break;
  709.  
  710.                         /* This is a carriage return. */
  711.  
  712.                     case 'R':
  713.  
  714.                         SharedBuffer[Count++] = '\r';
  715.                         break;
  716.  
  717.                         /* This is a tab. */
  718.  
  719.                     case 'T':
  720.  
  721.                         SharedBuffer[Count++] = '\t';
  722.                         break;
  723.  
  724.                         /* Send the current user name. */
  725.  
  726.                     case 'U':
  727.  
  728.                         if(UserName[0])
  729.                         {
  730.                             if(Count)
  731.                             {
  732.                                 (*SendLineLocal)(SharedBuffer,Count);
  733.  
  734.                                 Count = 0;
  735.                             }
  736.  
  737.                             (*SendLineLocal)(UserName,strlen(UserName));
  738.                         }
  739.  
  740.                         break;
  741.  
  742.                         /* Send a break across the serial line. */
  743.  
  744.                     case 'X':
  745.  
  746.                         if(Count)
  747.                         {
  748.                             (*SendLineLocal)(SharedBuffer,Count);
  749.  
  750.                             Count = 0;
  751.                         }
  752.  
  753.                         SendBreak();
  754.  
  755.                         break;
  756.  
  757.                         /* Feed the contents of the
  758.                          * clipboard into the input
  759.                          * stream.
  760.                          */
  761.  
  762.                     case 'I':
  763.  
  764.                         if(Count)
  765.                             (*SendLineLocal)(SharedBuffer,Count);
  766.  
  767.                         Count = LoadClip(SharedBuffer,256);
  768.  
  769.                         break;
  770.  
  771.                         /* Send a string to the clipboard. */
  772.  
  773.                     case 'G':
  774.  
  775.                         if(String[i + 1])
  776.                             SaveClip(&String[i + 1],strlen(&String[i + 1]));
  777.  
  778.                         return;
  779.  
  780.                         /* Send a string to the clipboard. */
  781.  
  782.                     case 'H':
  783.  
  784.                         if(String[i + 1])
  785.                             AddClip(&String[i + 1],strlen(&String[i + 1]));
  786.  
  787.                         return;
  788.  
  789.                         /* Produce the escape character. */
  790.  
  791.                     case 'E':
  792.  
  793.                         SharedBuffer[Count++] = ESC;
  794.                         break;
  795.  
  796.                         /* Call a menu item. */
  797.  
  798.                     case 'C':
  799.  
  800.                         i++;
  801.  
  802.                             /* Scan for a menu number or
  803.                              * a single quote...
  804.                              */
  805.  
  806.                         while(i < Len)
  807.                         {
  808.                             if(String[i] >= '0' && String[i] <= '9')
  809.                                 break;
  810.  
  811.                             if(String[i] == '\'')
  812.                                 break;
  813.  
  814.                             if(String[i] != ' ')
  815.                                 break;
  816.  
  817.                             i++;
  818.                         }
  819.  
  820.                         if(i < Len)
  821.                         {
  822.                             UBYTE DummyBuffer[256];
  823.  
  824.                                 /* Did we get a quote? */
  825.  
  826.                             if(String[i] == '\'')
  827.                             {
  828.                                 LONG Start = ++i;
  829.  
  830.                                 if(String[Start])
  831.                                 {
  832.                                     WORD Length;
  833.  
  834.                                     while(i < Len)
  835.                                     {
  836.                                         if(String[i] != '\'')
  837.                                             i++;
  838.                                         else
  839.                                             break;
  840.                                     }
  841.  
  842.                                     if(String[i] == '\'')
  843.                                         Length = i - Start;
  844.                                     else
  845.                                         Length = i - Start + 1;
  846.  
  847.                                     memcpy(DummyBuffer,&String[Start],Length);
  848.  
  849.                                     DummyBuffer[Length] = 0;
  850.  
  851.                                     CallMenu(DummyBuffer,0);
  852.                                 }
  853.                             }
  854.                             else
  855.                             {
  856.                                 if(String[i] >= '0' && String[i] <= '9')
  857.                                 {
  858.                                     LONG Start = i,Length;
  859.  
  860.                                     while(i < Len)
  861.                                     {
  862.                                         if(String[i] >= '0' && String[i] <= '9')
  863.                                             i++;
  864.                                         else
  865.                                             break;
  866.                                     }
  867.  
  868.                                     if(i == Start)
  869.                                         Length = 1;
  870.                                     else
  871.                                         Length = i - Start;
  872.  
  873.                                     memcpy(DummyBuffer,&String[Start],Length);
  874.  
  875.                                     DummyBuffer[Length] = 0;
  876.  
  877.                                     CallMenu(NULL,Atol(DummyBuffer));
  878.                                 }
  879.                             }
  880.                         }
  881.  
  882.                         break;
  883.  
  884.                         /* Insert the current dialing mode suffix. */
  885.  
  886.                     case 'W':
  887.  
  888.                         if(Config -> ModemConfig -> DialMode == DIALMODE_PULSE)
  889.                             SharedBuffer[Count++] = 'P';
  890.                         else
  891.                             SharedBuffer[Count++] = 'T';
  892.  
  893.                         break;
  894.  
  895.                         /* Stuff the character into the buffer. */
  896.  
  897.                     default:
  898.  
  899.                         SharedBuffer[Count++] = String[i];
  900.                         break;
  901.                 }
  902.  
  903.                 GotControl = FALSE;
  904.             }
  905.         }
  906.  
  907.             /* If the buffer is full, release it. */
  908.  
  909.         if(Count == 256)
  910.         {
  911.             (*SendLineLocal)(SharedBuffer,Count);
  912.  
  913.             Count = 0;
  914.         }
  915.     }
  916.  
  917.     if(Count)
  918.         (*SendLineLocal)(SharedBuffer,Count);
  919. }
  920.  
  921.     /* SerWriteVerbatim(APTR Buffer,LONG Size,BOOLEAN Echo):
  922.      *
  923.      *    The `no fancy features' version of SerWrite().
  924.      */
  925.  
  926. VOID __regargs
  927. SerWriteVerbatim(APTR Buffer,LONG Size,BOOLEAN Echo)
  928. {
  929.     if(XProtocolBase && (TransferBits & XPRS_USERMON))
  930.         Size = XProtocolUserMon(XprIO,Buffer,Size,Size);
  931.  
  932.     if(Size > 0)
  933.     {
  934.         if(Echo)
  935.         {
  936.             if(Marking)
  937.                 DropMarker();
  938.  
  939.             StartSerialWrite(Buffer,Size);
  940.  
  941.             ConProcess(Buffer,Size);
  942.  
  943.             WaitSerialWrite();
  944.         }
  945.         else
  946.             DoSerialWrite(Buffer,Size);
  947.  
  948.         BytesOut += Size;
  949.     }
  950. }
  951.  
  952.     /* SerWrite(APTR Buffer,LONG Size):
  953.      *
  954.      *    Send a number of bytes across the serial line.
  955.      */
  956.  
  957. VOID __regargs
  958. SerWrite(APTR Buffer,LONG Size)
  959. {
  960.     if(Size < 0)
  961.         Size = strlen(Buffer);
  962.  
  963.     if(Size)
  964.     {
  965.         if(RememberInput)
  966.         {
  967.             RememberInputText(Buffer,Size);
  968.  
  969.             if(((UBYTE *)Buffer)[Size - 1] == '\r' && RecordingLine)
  970.             {
  971.                 RememberSpill();
  972.  
  973.                 RecordingLine = FALSE;
  974.  
  975.                 RememberOutput = TRUE;
  976.                 RememberInput = FALSE;
  977.  
  978.                 CheckItem(MEN_RECORD_LINE,FALSE);
  979.             }
  980.         }
  981.         else
  982.         {
  983.             if(RememberOutput)
  984.             {
  985.                 RememberInputText(Buffer,Size);
  986.  
  987.                 RememberSpill();
  988.             }
  989.         }
  990.  
  991.         if(WriteRequest)
  992.         {
  993.             STATIC UBYTE __far TranslateData[512];
  994.  
  995.                 /* xpr wants to see the data before it is
  996.                  * transferred.
  997.                  */
  998.  
  999.             if(XProtocolBase && (TransferBits & XPRS_USERMON))
  1000.             {
  1001.                 if(Size = XProtocolUserMon(XprIO,Buffer,Size,Size))
  1002.                 {
  1003.                     if(SendTable)
  1004.                     {
  1005.                         struct TranslationHandle Handle;
  1006.  
  1007.                             /* Set up for buffer translation. */
  1008.  
  1009.                         TranslateSetup(&Handle,Buffer,Size,TranslateData,512,SendTable);
  1010.  
  1011.                             /* Full or half duplex? */
  1012.  
  1013.                         if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1014.                         {
  1015.                             if(SerWriteBypass)
  1016.                             {
  1017.                                     /* Process the data... */
  1018.  
  1019.                                 while(Size = TranslateBuffer(&Handle))
  1020.                                 {
  1021.                                     if((*SerWriteBypass)(TranslateData,Size))
  1022.                                     {
  1023.                                             /* ...and send it. */
  1024.  
  1025.                                         DoSerialWrite(TranslateData,Size);
  1026.                                     }
  1027.  
  1028.                                     BytesOut += Size;
  1029.                                 }
  1030.                             }
  1031.                             else
  1032.                             {
  1033.                                     /* Process the data... */
  1034.  
  1035.                                 while(Size = TranslateBuffer(&Handle))
  1036.                                 {
  1037.                                         /* ...and send it. */
  1038.  
  1039.                                     DoSerialWrite(TranslateData,Size);
  1040.  
  1041.                                     BytesOut += Size;
  1042.                                 }
  1043.                             }
  1044.                         }
  1045.                         else
  1046.                         {
  1047.                             if(Marking)
  1048.                                 DropMarker();
  1049.  
  1050.                             if(SerWriteBypass)
  1051.                             {
  1052.                                 while(Size = TranslateBuffer(&Handle))
  1053.                                 {
  1054.                                     if((*SerWriteBypass)(TranslateData,Size))
  1055.                                     {
  1056.                                             /* Process the data while it is transferred. */
  1057.  
  1058.                                         StartSerialWrite(TranslateData,Size);
  1059.  
  1060.                                         ConProcess(Buffer,Size);
  1061.  
  1062.                                         WaitSerialWrite();
  1063.                                     }
  1064.  
  1065.                                     BytesOut += Size;
  1066.                                 }
  1067.                             }
  1068.                             else
  1069.                             {
  1070.                                 while(Size = TranslateBuffer(&Handle))
  1071.                                 {
  1072.                                         /* Process the data while it is transferred. */
  1073.  
  1074.                                     StartSerialWrite(TranslateData,Size);
  1075.  
  1076.                                     ConProcess(TranslateData,Size);
  1077.  
  1078.                                     WaitSerialWrite();
  1079.  
  1080.                                     BytesOut += Size;
  1081.                                 }
  1082.                             }
  1083.                         }
  1084.                     }
  1085.                     else
  1086.                     {
  1087.                         if(SerWriteBypass)
  1088.                         {
  1089.                             if((*SerWriteBypass)(Buffer,Size))
  1090.                             {
  1091.                                     /* If full duplex is enabled, send the entire
  1092.                                      * buffer without processing.
  1093.                                      */
  1094.  
  1095.                                 if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1096.                                     DoSerialWrite(Buffer,Size);
  1097.                                 else
  1098.                                 {
  1099.                                         /* Process the data while it is transferred. */
  1100.  
  1101.                                     StartSerialWrite(Buffer,Size);
  1102.  
  1103.                                     if(Marking)
  1104.                                         DropMarker();
  1105.  
  1106.                                     ConProcess(Buffer,Size);
  1107.  
  1108.                                     WaitSerialWrite();
  1109.                                 }
  1110.                             }
  1111.                             else
  1112.                             {
  1113.                                 if(Config -> SerialConfig -> Duplex != DUPLEX_FULL)
  1114.                                 {
  1115.                                     if(Marking)
  1116.                                         DropMarker();
  1117.  
  1118.                                     ConProcess(Buffer,Size);
  1119.                                 }
  1120.                             }
  1121.                         }
  1122.                         else
  1123.                         {
  1124.                                 /* If full duplex is enabled, send the entire
  1125.                                  * buffer without processing.
  1126.                                  */
  1127.  
  1128.                             if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1129.                                 DoSerialWrite(Buffer,Size);
  1130.                             else
  1131.                             {
  1132.                                     /* Process the data while it is transferred. */
  1133.  
  1134.                                 StartSerialWrite(Buffer,Size);
  1135.  
  1136.                                 if(Marking)
  1137.                                     DropMarker();
  1138.  
  1139.                                 ConProcess(Buffer,Size);
  1140.  
  1141.                                 WaitSerialWrite();
  1142.                             }
  1143.                         }
  1144.  
  1145.                         BytesOut += Size;
  1146.                     }
  1147.                 }
  1148.             }
  1149.             else
  1150.             {
  1151.                 if(SendTable)
  1152.                 {
  1153.                     struct TranslationHandle Handle;
  1154.  
  1155.                         /* Set up for buffer translation. */
  1156.  
  1157.                     TranslateSetup(&Handle,Buffer,Size,TranslateData,512,SendTable);
  1158.  
  1159.                         /* Full or half duplex? */
  1160.  
  1161.                     if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1162.                     {
  1163.                         if(SerWriteBypass)
  1164.                         {
  1165.                                 /* Process the data... */
  1166.  
  1167.                             while(Size = TranslateBuffer(&Handle))
  1168.                             {
  1169.                                 if((*SerWriteBypass)(TranslateData,Size))
  1170.                                 {
  1171.                                         /* ...and send it. */
  1172.  
  1173.                                     DoSerialWrite(TranslateData,Size);
  1174.                                 }
  1175.  
  1176.                                 BytesOut += Size;
  1177.                             }
  1178.                         }
  1179.                         else
  1180.                         {
  1181.                                 /* Process the data... */
  1182.  
  1183.                             while(Size = TranslateBuffer(&Handle))
  1184.                             {
  1185.                                     /* ...and send it. */
  1186.  
  1187.                                 DoSerialWrite(TranslateData,Size);
  1188.  
  1189.                                 BytesOut += Size;
  1190.                             }
  1191.                         }
  1192.                     }
  1193.                     else
  1194.                     {
  1195.                         if(Marking)
  1196.                             DropMarker();
  1197.  
  1198.                         if(SerWriteBypass)
  1199.                         {
  1200.                             while(Size = TranslateBuffer(&Handle))
  1201.                             {
  1202.                                 if((*SerWriteBypass)(TranslateData,Size))
  1203.                                 {
  1204.                                         /* Process the data while it is transferred. */
  1205.  
  1206.                                     StartSerialWrite(TranslateData,Size);
  1207.  
  1208.                                     ConProcess(TranslateData,Size);
  1209.  
  1210.                                     WaitSerialWrite();
  1211.                                 }
  1212.  
  1213.                                 BytesOut += Size;
  1214.                             }
  1215.                         }
  1216.                         else
  1217.                         {
  1218.                             while(Size = TranslateBuffer(&Handle))
  1219.                             {
  1220.                                     /* Process the data while it is transferred. */
  1221.  
  1222.                                 StartSerialWrite(TranslateData,Size);
  1223.  
  1224.                                 ConProcess(TranslateData,Size);
  1225.  
  1226.                                 WaitSerialWrite();
  1227.  
  1228.                                 BytesOut += Size;
  1229.                             }
  1230.                         }
  1231.                     }
  1232.                 }
  1233.                 else
  1234.                 {
  1235.                     if(SerWriteBypass)
  1236.                     {
  1237.                         if((*SerWriteBypass)(Buffer,Size))
  1238.                         {
  1239.                                 /* If full duplex is enabled, send the entire
  1240.                                  * buffer without processing.
  1241.                                  */
  1242.  
  1243.                             if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1244.                                 DoSerialWrite(Buffer,Size);
  1245.                             else
  1246.                             {
  1247.                                     /* Process the data while it is transferred. */
  1248.  
  1249.                                 StartSerialWrite(Buffer,Size);
  1250.  
  1251.                                 if(Marking)
  1252.                                     DropMarker();
  1253.  
  1254.                                 ConProcess(Buffer,Size);
  1255.  
  1256.                                 WaitSerialWrite();
  1257.                             }
  1258.                         }
  1259.                     }
  1260.                     else
  1261.                     {
  1262.                             /* If full duplex is enabled, send the entire
  1263.                              * buffer without processing.
  1264.                              */
  1265.  
  1266.                         if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1267.                             DoSerialWrite(Buffer,Size);
  1268.                         else
  1269.                         {
  1270.                                 /* Process the data while it is transferred. */
  1271.  
  1272.                             StartSerialWrite(Buffer,Size);
  1273.  
  1274.                             if(Marking)
  1275.                                 DropMarker();
  1276.  
  1277.                             ConProcess(Buffer,Size);
  1278.  
  1279.                             WaitSerialWrite();
  1280.                         }
  1281.                     }
  1282.  
  1283.                     BytesOut += Size;
  1284.                 }
  1285.             }
  1286.         }
  1287.     }
  1288. }
  1289.  
  1290.     /* RestartSerial():
  1291.      *
  1292.      *    Restart read/write activity on the serial line.
  1293.      */
  1294.  
  1295. VOID __regargs
  1296. RestartSerial(BYTE Override)
  1297. {
  1298.     StartSerialRead(ReadBuffer,1);
  1299. }
  1300.  
  1301.     /* ClearSerial():
  1302.      *
  1303.      *    Terminate all read/write activity on the serial line.
  1304.      */
  1305.  
  1306. VOID
  1307. ClearSerial()
  1308. {
  1309.     StopSerialRead();
  1310.  
  1311.     DoSerialCmd(CMD_CLEAR);
  1312. }
  1313.  
  1314.     /* DeleteSerial():
  1315.      *
  1316.      *    Close the serial device and release all associated
  1317.      *    resources.
  1318.      */
  1319.  
  1320. VOID
  1321. DeleteSerial()
  1322. {
  1323.     BYTE Closed = FALSE;
  1324.  
  1325.     if(ReadRequest)
  1326.     {
  1327.         if(ReadRequest -> IOSer . io_Device)
  1328.         {
  1329.             struct Device    *Device;
  1330.             UBYTE         Name[MAX_FILENAME_LENGTH];
  1331.  
  1332.             StopSerialRead();
  1333.  
  1334.             if(!Config -> SerialConfig -> Shared)
  1335.             {
  1336.                 ReadRequest -> IOSer . io_Command = CMD_RESET;
  1337.  
  1338.                 DoIO(ReadRequest);
  1339.             }
  1340.  
  1341.             strcpy(Name,ReadRequest -> IOSer . io_Device -> dd_Library . lib_Node . ln_Name);
  1342.  
  1343.             CloseDevice(ReadRequest);
  1344.  
  1345.             Forbid();
  1346.  
  1347.             if(Device = (struct Device *)FindName(&SysBase -> DeviceList,Name))
  1348.                 RemDevice(Device);
  1349.  
  1350.             Permit();
  1351.  
  1352.             Closed = TRUE;
  1353.         }
  1354.  
  1355.         DeleteIORequest(ReadRequest);
  1356.  
  1357.         ReadRequest = NULL;
  1358.     }
  1359.  
  1360.     if(WriteRequest)
  1361.     {
  1362.         if(WriteRequest -> IOSer . io_Device && !Closed)
  1363.         {
  1364.             struct Device    *Device;
  1365.             UBYTE         Name[MAX_FILENAME_LENGTH];
  1366.  
  1367.             StopSerialWrite();
  1368.  
  1369.             strcpy(Name,WriteRequest -> IOSer . io_Device -> dd_Library . lib_Node . ln_Name);
  1370.  
  1371.             CloseDevice(WriteRequest);
  1372.  
  1373.             Forbid();
  1374.  
  1375.             if(Device = (struct Device *)FindName(&SysBase -> DeviceList,Name))
  1376.                 RemDevice(Device);
  1377.  
  1378.             Permit();
  1379.         }
  1380.  
  1381.         if(WriteRequest -> IOSer . io_Message . mn_ReplyPort)
  1382.             DeleteMsgPort(WriteRequest -> IOSer . io_Message . mn_ReplyPort);
  1383.  
  1384.         DeleteIORequest(WriteRequest);
  1385.  
  1386.         WriteRequest = NULL;
  1387.     }
  1388.  
  1389.     if(ReadPort)
  1390.     {
  1391.         DeleteMsgPort(ReadPort);
  1392.  
  1393.         ReadPort = NULL;
  1394.     }
  1395.  
  1396.     if(ReadBuffer)
  1397.     {
  1398.         FreeVecPooled(ReadBuffer);
  1399.  
  1400.         ReadBuffer = NULL;
  1401.     }
  1402.  
  1403.     if(HostReadBuffer)
  1404.     {
  1405.         FreeVecPooled(HostReadBuffer);
  1406.  
  1407.         HostReadBuffer = NULL;
  1408.     }
  1409.  
  1410.     if(StripBuffer)
  1411.     {
  1412.         FreeVecPooled(StripBuffer);
  1413.  
  1414.         StripBuffer = NULL;
  1415.     }
  1416.  
  1417.     if(OwnDevUnitBase && SerialDevice[0] && UnitNumber != -1 && SerialLocked)
  1418.     {
  1419.         FreeDevUnit(SerialDevice,UnitNumber);
  1420.  
  1421.         SerialDevice[0] = 0;
  1422.  
  1423.         UnitNumber = -1;
  1424.     }
  1425.  
  1426.     if(OwnDevBit != -1)
  1427.     {
  1428.         FreeSignal(OwnDevBit);
  1429.  
  1430.         OwnDevBit = -1;
  1431.     }
  1432.  
  1433.     SerialLocked = FALSE;
  1434.  
  1435.     ResetSerialRead();
  1436.     ResetSerialWrite();
  1437. }
  1438.  
  1439.     /* GetSerialError(LONG Error,BYTE *Reset):
  1440.      *
  1441.      *    Return an error message for each possible serial device error.
  1442.      */
  1443.  
  1444. STRPTR __regargs
  1445. GetSerialError(LONG Error,BYTE *Reset)
  1446. {
  1447.     if(Reset)
  1448.         *Reset = FALSE;
  1449.  
  1450.     switch(Error)
  1451.     {
  1452.         case IOERR_BADLENGTH:
  1453.         case IOERR_BADADDRESS:
  1454.         case IOERR_SELFTEST:
  1455.         case IOERR_OPENFAIL:
  1456.  
  1457.             return(LocaleString(MSG_SERIAL_OPENFAILURE_TXT));
  1458.  
  1459.         case SerErr_DevBusy:
  1460.  
  1461.             return(LocaleString(MSG_SERIAL_ERROR_DEVBUSY_TXT));
  1462.  
  1463.         case SerErr_BaudMismatch:
  1464.  
  1465.             if(Reset)
  1466.                 *Reset = TRUE;
  1467.  
  1468.             return(LocaleString(MSG_SERIAL_ERROR_BAUDMISMATCH_TXT));
  1469.  
  1470.         case SerErr_BufErr:
  1471.  
  1472.             if(Reset)
  1473.                 *Reset = TRUE;
  1474.  
  1475.             return(LocaleString(MSG_SERIAL_ERROR_BUFERR_TXT));
  1476.  
  1477.         case SerErr_InvParam:
  1478.  
  1479.             if(Reset)
  1480.                 *Reset = TRUE;
  1481.  
  1482.             return(LocaleString(MSG_SERIAL_ERROR_INVPARAM_TXT));
  1483.  
  1484.         case SerErr_LineErr:
  1485.  
  1486.             return(LocaleString(MSG_SERIAL_ERROR_LINEERR_TXT));
  1487.  
  1488.         case SerErr_ParityErr:
  1489.  
  1490.             if(Reset)
  1491.                 *Reset = TRUE;
  1492.  
  1493.             return(LocaleString(MSG_SERIAL_ERROR_PARITYERR_TXT));
  1494.  
  1495.         case SerErr_TimerErr:
  1496.  
  1497.             return(LocaleString(MSG_SERIAL_ERROR_TIMERERR_TXT));
  1498.  
  1499.         case SerErr_BufOverflow:
  1500.  
  1501.             if(Reset)
  1502.                 *Reset = TRUE;
  1503.  
  1504.             return(LocaleString(MSG_SERIAL_ERROR_BUFOVERFLOW_TXT));
  1505.  
  1506.         case SerErr_NoDSR:
  1507.  
  1508.             return(LocaleString(MSG_SERIAL_ERROR_NODSR_TXT));
  1509.  
  1510.     /*    case SerErr_UnitBusy:    */
  1511.         case IOERR_UNITBUSY:
  1512.         case 16:
  1513.  
  1514.             return(LocaleString(MSG_SERIAL_ERROR_UNIT_BUSY_TXT));
  1515.  
  1516.         default:
  1517.  
  1518.             return(NULL);
  1519.     }
  1520. }
  1521.  
  1522.     /* CreateSerial():
  1523.      *
  1524.      *    Create handles for the serial device and open it.
  1525.      */
  1526.  
  1527. STRPTR
  1528. CreateSerial()
  1529. {
  1530.     struct MsgPort *WritePort;
  1531.  
  1532.         /* If OwnDevUnit.library is available, try to lock
  1533.          * the serial driver.
  1534.          */
  1535.  
  1536. Start:    if(OwnDevUnitBase && Config -> SerialConfig -> UseOwnDevUnit)
  1537.     {
  1538.         STRPTR Error;
  1539.  
  1540.             /* Allocate a signal for OwnDevUnit, in case we might need it. */
  1541.  
  1542.         if(OwnDevBit == -1)
  1543.         {
  1544.             OwnDevBit = AllocSignal(-1);
  1545.  
  1546.                 /* Attempt to lock the device unit... */
  1547.  
  1548.             Error = AttemptDevUnit(Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,TermIDString,OwnDevBit == -1 ? NULL : OwnDevBit);
  1549.         }
  1550.         else
  1551.             Error = NULL;
  1552.  
  1553.         if(Error)
  1554.         {
  1555.                 /* Check for error type if any. */
  1556.  
  1557.             if(!Strnicmp(Error,ODUERR_LEADCHAR,1))
  1558.                 SPrintf(SharedBuffer,LocaleString(MSG_SERIAL_ERROR_ACCESSING_TXT),Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,&Error[1]);
  1559.             else
  1560.                 SPrintf(SharedBuffer,LocaleString(MSG_SERIAL_DEVICE_IN_USE_TXT),Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,Error);
  1561.  
  1562.             SerialDevice[0] = 0;
  1563.  
  1564.             UnitNumber = -1;
  1565.  
  1566.             SerialLocked = FALSE;
  1567.  
  1568.             return(SharedBuffer);
  1569.         }
  1570.         else
  1571.         {
  1572.             strcpy(SerialDevice,Config -> SerialConfig -> SerialDevice);
  1573.  
  1574.             UnitNumber = Config -> SerialConfig -> UnitNumber;
  1575.  
  1576.             SerialLocked = TRUE;
  1577.         }
  1578.     }
  1579.     else
  1580.         SerialLocked = FALSE;
  1581.  
  1582.     Update_CR_LF_Translation();
  1583.  
  1584.     if(ReadBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY))
  1585.     {
  1586.         if(StripBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY))
  1587.         {
  1588.             if(XProtocolBase && (TransferBits & XPRS_HOSTNOWAIT))
  1589.                 HostReadBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY);
  1590.             else
  1591.                 HostReadBuffer = NULL;
  1592.  
  1593.             if(ReadPort = (struct MsgPort *)CreateMsgPort())
  1594.             {
  1595.                 if(ReadRequest = (struct IOExtSer *)CreateIORequest(ReadPort,sizeof(struct IOExtSer)))
  1596.                 {
  1597.                     LONG Error;
  1598.  
  1599.                     SetSerialReadAttributes(
  1600.                         SERA_Baud,        Config -> SerialConfig -> BaudRate,
  1601.                         SERA_BreakTime,        Config -> SerialConfig -> BreakLength,
  1602.                         SERA_BitsPerChar,    Config -> SerialConfig -> BitsPerChar,
  1603.                         SERA_StopBits,        Config -> SerialConfig -> StopBits,
  1604.                         SERA_BufferSize,    Config -> SerialConfig -> SerialBufferSize,
  1605.                         SERA_Parity,        Config -> SerialConfig -> Parity,
  1606.                         SERA_Handshaking,    Config -> SerialConfig -> HandshakingProtocol,
  1607.                         SERA_HighSpeed,        Config -> SerialConfig -> HighSpeed,
  1608.                         SERA_Shared,        Config -> SerialConfig -> Shared,
  1609.                     TAG_DONE);
  1610.  
  1611.                     if(!(Error = OpenDevice(Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,ReadRequest,0)))
  1612.                     {
  1613.                         if(WritePort = (struct MsgPort *)CreateMsgPort())
  1614.                         {
  1615.                             if(WriteRequest = (struct IOExtSer *)CreateIORequest(WritePort,sizeof(struct IOExtSer)))
  1616.                             {
  1617.                                 CopyMem(ReadRequest,WriteRequest,sizeof(struct IOExtSer));
  1618.  
  1619.                                 WriteRequest -> IOSer . io_Message . mn_ReplyPort = WritePort;
  1620.  
  1621.                                 ResetSerialRead();
  1622.                                 ResetSerialWrite();
  1623.  
  1624.                                 SetFlags();
  1625.  
  1626.                                 /* If RTS/CTS (7 wire handshaking) is
  1627.                                  * selected, have a look at the DSR
  1628.                                  * line to see whether the modem
  1629.                                  * connected is willing to support
  1630.                                  * this handshaking mode.
  1631.                                  */
  1632.  
  1633.                                 if(Config -> SerialConfig -> HandshakingProtocol == HANDSHAKING_RTSCTS_DSR)
  1634.                                 {
  1635.                                     if(FirstInvocation)
  1636.                                     {
  1637.                                         DeleteSerial();
  1638.  
  1639.                                         Config -> SerialConfig -> HandshakingProtocol = HANDSHAKING_NONE;
  1640.  
  1641.                                         goto Start;
  1642.                                     }
  1643.                                     else
  1644.                                     {
  1645.                                             /* If the line happens to
  1646.                                              * be high, there is no
  1647.                                              * DSR signal present.
  1648.                                              */
  1649.  
  1650. Retry:                                        if(GetSerialStatus() & CIAF_COMDSR)
  1651.                                         {
  1652.                                             if(MyEasyRequest(Window,LocaleString(MSG_SERIAL_NO_DSR_SIGNAL_TXT),LocaleString(MSG_SERIAL_RETRY_CANCEL_TXT)))
  1653.                                                 goto Retry;
  1654.                                             else
  1655.                                             {
  1656.                                                 DeleteSerial();
  1657.  
  1658.                                                 Config -> SerialConfig -> HandshakingProtocol = HANDSHAKING_NONE;
  1659.  
  1660.                                                 SerialMessage = LocaleString(MSG_SERIAL_NO_DSR_SIGNAL_HANDSHAKING_DISABLED_TXT);
  1661.  
  1662.                                                 goto Start;
  1663.                                             }
  1664.                                         }
  1665.                                     }
  1666.                                 }
  1667.  
  1668.                                 RestartSerial(FALSE);
  1669.  
  1670.                                 return(NULL);
  1671.                             }
  1672.                         }
  1673.                         else
  1674.                         {
  1675.                             SerialMessage = NULL;
  1676.  
  1677.                             DeleteSerial();
  1678.  
  1679.                             return(LocaleString(MSG_SERIAL_FAILED_TO_CREATE_WRITE_PORT_TXT));
  1680.                         }
  1681.                     }
  1682.                     else
  1683.                     {
  1684.                         STRPTR String;
  1685.  
  1686.                         ReadRequest -> IOSer . io_Device = NULL;
  1687.  
  1688.                         DeleteSerial();
  1689.  
  1690.                         SerialMessage = NULL;
  1691.  
  1692.                         if(!(String = GetSerialError(Error,NULL)))
  1693.                             String = LocaleString(MSG_SERIAL_ERROR_DEVBUSY_TXT);
  1694.  
  1695.                         SPrintf(SharedBuffer,String,Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber);
  1696.  
  1697.                         return(SharedBuffer);
  1698.                     }
  1699.                 }
  1700.             }
  1701.             else
  1702.             {
  1703.                 SerialMessage = NULL;
  1704.  
  1705.                 DeleteSerial();
  1706.  
  1707.                 return(LocaleString(MSG_SERIAL_FAILED_TO_CREATE_READ_PORT_TXT));
  1708.             }
  1709.         }
  1710.     }
  1711.  
  1712.     SerialMessage = NULL;
  1713.  
  1714.     DeleteSerial();
  1715.  
  1716.     return(LocaleString(MSG_SERIAL_NOT_ENOUGH_MEMORY_TXT));
  1717. }
  1718.  
  1719.     /* ReconfigureSerial(struct Window *Window,struct SerialSettings *SerialConfig):
  1720.      *
  1721.      *    Reconfigure the serial driver according to the new
  1722.      *    serial settings.
  1723.      */
  1724.  
  1725. BYTE __regargs
  1726. ReconfigureSerial(struct Window *Window,struct SerialSettings *SerialConfig)
  1727. {
  1728.     BYTE    Success    = RECONFIGURE_NOCHANGE,
  1729.         IsNew;
  1730.  
  1731.         /* Are new settings to be installed or have they already
  1732.          * been copied to the correct locations?
  1733.          */
  1734.  
  1735.     if(SerialConfig)
  1736.     {
  1737.         if(memcmp(Config -> SerialConfig,SerialConfig,sizeof(struct SerialSettings)))
  1738.             IsNew = TRUE;
  1739.         else
  1740.             IsNew = FALSE;
  1741.     }
  1742.     else
  1743.     {
  1744.         if(memcmp(Config -> SerialConfig,PrivateConfig -> SerialConfig,sizeof(struct SerialSettings)))
  1745.             IsNew = TRUE;
  1746.         else
  1747.             IsNew = FALSE;
  1748.     }
  1749.  
  1750.         /* Any changes in the serial configuration area? */
  1751.  
  1752.     if(IsNew)
  1753.     {
  1754.         BYTE SameDevice = TRUE;
  1755.  
  1756.             /* Any new data to swap in? */
  1757.  
  1758.         if(SerialConfig)
  1759.         {
  1760.                 /* Store the previous settings. */
  1761.  
  1762.             SaveConfig(Config,PrivateConfig);
  1763.  
  1764.                 /* Install the new settings. */
  1765.  
  1766.             memcpy(Config -> SerialConfig,SerialConfig,sizeof(struct SerialSettings));
  1767.         }
  1768.  
  1769.             /* Any device name or unit number change? */
  1770.  
  1771.         if(strcmp(PrivateConfig -> SerialConfig -> SerialDevice,Config -> SerialConfig -> SerialDevice) || PrivateConfig -> SerialConfig -> UnitNumber != Config -> SerialConfig -> UnitNumber || PrivateConfig -> SerialConfig -> UseOwnDevUnit != Config -> SerialConfig -> UseOwnDevUnit)
  1772.             SameDevice = FALSE;
  1773.         else
  1774.         {
  1775.                 /* Handshaking mode changed to RTS/CTS protocol? */
  1776.  
  1777.             if((PrivateConfig -> SerialConfig -> HandshakingProtocol == HANDSHAKING_NONE && Config -> SerialConfig -> HandshakingProtocol != HANDSHAKING_NONE) || (PrivateConfig -> SerialConfig -> HandshakingProtocol != HANDSHAKING_NONE && Config -> SerialConfig -> HandshakingProtocol == HANDSHAKING_NONE))
  1778.                 SameDevice = FALSE;
  1779.         }
  1780.  
  1781.             /* Stop any IO activity. */
  1782.  
  1783.         if(ReadRequest)
  1784.             ClearSerial();
  1785.         else
  1786.             SameDevice = FALSE;
  1787.  
  1788.             /* No dramatic changes? Simply change the parameters. */
  1789.  
  1790.         if(SameDevice)
  1791.         {
  1792.             LONG Error;
  1793.  
  1794.             if(PrivateConfig -> SerialConfig -> SerialBufferSize != Config -> SerialConfig -> SerialBufferSize)
  1795.             {
  1796.                 STRPTR NewReadBuffer;
  1797.  
  1798.                 if(NewReadBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY))
  1799.                 {
  1800.                     STRPTR NewStripBuffer;
  1801.  
  1802.                     if(NewStripBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY))
  1803.                     {
  1804.                         FreeVecPooled(ReadBuffer);
  1805.                         FreeVecPooled(StripBuffer);
  1806.  
  1807.                         ReadBuffer    = NewReadBuffer;
  1808.                         StripBuffer    = NewStripBuffer;
  1809.                     }
  1810.                     else
  1811.                     {
  1812.                         FreeVecPooled(NewReadBuffer);
  1813.  
  1814.                         Config -> SerialConfig -> SerialBufferSize = PrivateConfig -> SerialConfig -> SerialBufferSize;
  1815.                     }
  1816.                 }
  1817.                 else
  1818.                     Config -> SerialConfig -> SerialBufferSize = PrivateConfig -> SerialConfig -> SerialBufferSize;
  1819.             }
  1820.  
  1821.             Update_CR_LF_Translation();
  1822.  
  1823.                 /* Use new parameters... */
  1824.  
  1825.             Error = SetFlags();
  1826.  
  1827.                 /* Trouble? */
  1828.  
  1829.             if(Error)
  1830.             {
  1831.                 STRPTR    String;
  1832.                 BYTE    Reset;
  1833.  
  1834.                     /* Query the error message. */
  1835.  
  1836.                 if(!(String = GetSerialError(Error,&Reset)))
  1837.                     String = LocaleString(MSG_SERIAL_ERROR_DEVBUSY_TXT);
  1838.  
  1839.                     /* Build the device name/unit number string. */
  1840.  
  1841.                 SPrintf(SharedBuffer,String,Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber);
  1842.  
  1843.                     /* Display the error requester. */
  1844.  
  1845.                 LT_LockWindow(Window);
  1846.  
  1847.                 MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),SharedBuffer);
  1848.  
  1849.                 LT_UnlockWindow(Window);
  1850.  
  1851.                     /* Is a serial driver reset required? */
  1852.  
  1853.                 if(Reset)
  1854.                 {
  1855.                         /* Execute the reset command. */
  1856.  
  1857.                     DoSerialCmd(CMD_RESET);
  1858.  
  1859.                         /* Copy the serial driver settings
  1860.                          * to the global configuration.
  1861.                          */
  1862.  
  1863.                     CopyWriteFlags();
  1864.  
  1865.                         /* Also set the read request driver
  1866.                          * flags.
  1867.                          */
  1868.  
  1869.                     SetFlags();
  1870.                 }
  1871.             }
  1872.  
  1873.                 /* Restart read request. */
  1874.  
  1875.             RestartSerial(FALSE);
  1876.         }
  1877.         else
  1878.         {
  1879.             STRPTR Error;
  1880.  
  1881.                 /* Shut down the serial driver. */
  1882.  
  1883.             DeleteSerial();
  1884.  
  1885.                 /* Reinitialize the serial driver. */
  1886.  
  1887.             if(!(Error = CreateSerial()))
  1888.             {
  1889.                     /* Free the work buffer. */
  1890.  
  1891.                 if(StripBuffer)
  1892.                     FreeVecPooled(StripBuffer);
  1893.  
  1894.                     /* Try to allocate a new serial data work buffer. */
  1895.  
  1896.                 if(!(StripBuffer = (STRPTR)AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY)))
  1897.                     Error = LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT);
  1898.             }
  1899.  
  1900.                 /* Trouble? */
  1901.  
  1902.             if(Error)
  1903.             {
  1904.                     /* Display the error requester. */
  1905.  
  1906.                 LT_LockWindow(Window);
  1907.  
  1908.                 MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Error);
  1909.  
  1910.                     /* Clean up the mess :-( */
  1911.  
  1912.                 DeleteSerial();
  1913.  
  1914.                 LT_UnlockWindow(Window);
  1915.  
  1916.                 Success = RECONFIGURE_FAILURE;
  1917.             }
  1918.             else
  1919.             {
  1920.                     /* Are we to display an error message? */
  1921.  
  1922.                 if(SerialMessage)
  1923.                 {
  1924.                     MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),SerialMessage);
  1925.  
  1926.                     SerialMessage = NULL;
  1927.                 }
  1928.             }
  1929.         }
  1930.     }
  1931.  
  1932.     return(Success);
  1933. }
  1934.  
  1935.     /* ReopenSerial():
  1936.      *
  1937.      *    Reopen the serial driver.
  1938.      */
  1939.  
  1940. VOID
  1941. ReopenSerial()
  1942. {
  1943.     BYTE    SerialClosed = TRUE;
  1944.     STRPTR    Error;
  1945.  
  1946.     do
  1947.     {
  1948.         if(Error = CreateSerial())
  1949.         {
  1950.             DeleteSerial();
  1951.  
  1952.             switch(MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_TERMMAIN_RETRY_IGNORE_QUIT_TXT),Error))
  1953.             {
  1954.                 case 2:    SerialClosed = FALSE;
  1955.                     break;
  1956.  
  1957.                 case 0:    MainTerminated = TRUE;
  1958.                     break;
  1959.             }
  1960.         }
  1961.         else
  1962.         {
  1963.             SerialClosed = FALSE;
  1964.  
  1965.             if(SerialMessage)
  1966.             {
  1967.                 MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),SerialMessage);
  1968.  
  1969.                 SerialMessage = NULL;
  1970.             }
  1971.         }
  1972.     }
  1973.     while(SerialClosed);
  1974. }
  1975.  
  1976.     /* HandleSerial():
  1977.      *
  1978.      *    Handle the data coming in from the serial line.
  1979.      */
  1980.  
  1981. BYTE
  1982. HandleSerial()
  1983. {
  1984.     BYTE MoreData = FALSE;
  1985.  
  1986.     if(ReadPort && !ReleaseSerial && Status != STATUS_HOLDING)
  1987.     {
  1988.         if(HostReadBuffer)
  1989.         {
  1990.             LONG Length = XProtocolHostMon(XprIO,HostReadBuffer,0,SerialBufferSize);
  1991.  
  1992.             if(Translate_CR_LF)
  1993.                 Length = (* Translate_CR_LF)(HostReadBuffer,Length);
  1994.  
  1995.             if(Length)
  1996.             {
  1997.                 if(Marking)
  1998.                     DropMarker();
  1999.  
  2000.                 ConProcess(HostReadBuffer,Length);
  2001.  
  2002.                 if(Status == STATUS_HOLDING)
  2003.                     return(FALSE);
  2004.             }
  2005.  
  2006.             MoreData = TRUE;
  2007.         }
  2008.  
  2009.         if(DataHold)
  2010.         {
  2011.             register STRPTR    Data    = DataHold;
  2012.             register LONG    Size    = DataSize;
  2013.  
  2014.             DataHold = NULL;
  2015.  
  2016.             if(Marking)
  2017.                 DropMarker();
  2018.  
  2019.             (* ConTransfer)(Data,Size);
  2020.  
  2021.             MoreData = TRUE;
  2022.  
  2023.             RestartSerial(FALSE);
  2024.  
  2025.             return(MoreData);
  2026.         }
  2027.  
  2028.             /* Any news? */
  2029.  
  2030.         if(CheckSerialRead())
  2031.         {
  2032.             MoreData = TRUE;
  2033.  
  2034.             if(!WaitSerialRead())
  2035.             {
  2036.                 if(Marking)
  2037.                     DropMarker();
  2038.  
  2039.                 BytesIn++;
  2040.  
  2041.                 if(Translate_CR_LF)
  2042.                 {
  2043.                     register LONG Size = (* Translate_CR_LF)(ReadBuffer,1);
  2044.  
  2045.                     if(Size)
  2046.                         (* ConTransfer)(ReadBuffer,Size);
  2047.                 }
  2048.                 else
  2049.                     (* ConTransfer)(ReadBuffer,1);
  2050.  
  2051.                 if(Status != STATUS_HOLDING && !DataHold)
  2052.                 {
  2053.                     ULONG Length;
  2054.  
  2055.                         /* Check how many bytes are still in
  2056.                          * the serial buffer.
  2057.                          */
  2058.  
  2059.                     if(Length = GetSerialWaiting())
  2060.                     {
  2061.                         ULONG Max = SerialBufferSize;
  2062.  
  2063.                         if(Max > Config -> SerialConfig -> Quantum)
  2064.                             Max = Config -> SerialConfig -> Quantum;
  2065.  
  2066.                         if(Length > Max)
  2067.                             Length = Max;
  2068.  
  2069.                         if(!DoSerialRead(ReadBuffer,Length))
  2070.                         {
  2071.                             BytesIn += Length;
  2072.  
  2073.                             if(Translate_CR_LF)
  2074.                             {
  2075.                                 if(Length = (* Translate_CR_LF)(ReadBuffer,Length))
  2076.                                     (* ConTransfer)(ReadBuffer,Length);
  2077.                             }
  2078.                             else
  2079.                                 (* ConTransfer)(ReadBuffer,Length);
  2080.                         }
  2081.                     }
  2082.                 }
  2083.             }
  2084.  
  2085.             if(DataHold)
  2086.                 ClrSignal(SIG_SERIAL);
  2087.             else
  2088.                 RestartSerial(FALSE);
  2089.         }
  2090.     }
  2091.  
  2092.     return(MoreData);
  2093. }
  2094.